<template>
  <el-dialog :title="!dataForm.id ? '新增' : '修改'" :close-on-click-modal="false" :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="120px">
      <el-form-item label="帮助标题" prop="title">
        <el-input maxlength="100" v-model="dataForm.title"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input type="textarea" maxlength="1000" :rows="10" v-model="dataForm.content"></el-input>
      </el-form-item>
      <el-form-item label="排序号" prop="sort">
        <el-input-number v-model="dataForm.sort" :precision="0" :max="999" :min="0" label="排序号"></el-input-number>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import seviceApi from '@/api/serviceApi.js'
import { treeDataTranslate } from '@/utils'
import editor from '@/components/editor'
export default {
  components: {
    editor
  },
  data () {
    return {
      visible: false,
      dataForm: {
        id: '0',
        title: '',
        content: '',
        sort: ''
      },
      dataRule: {
        title: [
          { required: true, message: '标题不能为空', trigger: 'blur' }
        ],
        content: [
          { required: true, message: '内容不能为空', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '排序号不能为空', trigger: 'blur' }
        ]
      }
    }
  },

  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
      })
      this.dataForm.content = ''
      if (id) {
        seviceApi.faqInfo(id).then(res => {
          if (res && res.data.code == 200) {
            this.dataForm = res.data.data
          }
        })
      }
    },
    // 编辑器
    getTxt (e) {
      this.dataForm.content = e
    },
    dataFormSubmit () {
      if (!this.dataForm.id) {
        delete this.dataForm.id
        seviceApi.saveFaq(this.dataForm).then(res => {
          if (res && res.data.code == 200) {
            this.$message({
              message: '添加成功',
              type: 'success'
            })
            this.visible = false
            this.$emit('updataList', 1)
          }
        })
      } else {
        seviceApi.updateFaq(this.dataForm).then(res => {
          if (res && res.data.code == 200) {
            this.$message({
              message: '修改成功',
              type: 'success'
            })
            this.visible = false
            this.$emit('updataList', 1)
          }
        })
      }
    }
  }
}
</script>

<style lang="scss">
</style>
